<?php
/**
 * PortalController.class.php
 */

class PortalController Extends LiriopeController {

  public function login( $params=NULL ) {
    if(self::isLoggedIn()) {
      go('/board-of-directors/resources');
    }
    $page = $this->getPage();
    $page->errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : NULL;
    unset($_SESSION['errors']);
  }

  public function verify( $params=NULL ) {
    // check if they are logged in
    if(self::isLoggedIn()) {
      go('/board-of-directors/resources');
    }
    // otherwise, check the posted form
    $attempt = md5(r::get('password'));
    if( $attempt === c::get('portal-password')) {
      self::isLoggedIn(true);
      go('/board-of-directors/resources');
    }
    // send them back to try again
    $_SESSION['errors']['password'] = "Incorrect password";
    go('/board-of-directors/portal');
  }

  public function resources($params=NULL) {
    if(!self::isLoggedIn()) {
      go('/board-of-directors/portal');
    }
  }

  public function logout($params=NULL) {
    self::isLoggedIn(false);
    session_destroy('board.portal');
  }

  private static function isLoggedIn($force=NULL) {
    session_start('board.portal');
    if($force !== NULL && $force) {
      $_SESSION['board.validated'] = 1;
      return true;
    }
    if($force !== NULL && !$force) {
      $_SESSION['board.validated'] = 0;
      return false;
    }
    if(isset($_SESSION['board.validated']) && $_SESSION['board.validated']) return true;
    return false;
  }

}
